<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="名称" prop="dname">
        <el-input v-model="drawerProps.row!.dname" placeholder="请填写名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="drawerProps.row!.description" placeholder="请填写描述" clearable></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="drawerProps.row!.remark" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
      <el-form-item label="背景颜色" prop="color">
        <el-input type="color" v-model="drawerProps.row!.color"> </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { genderType } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import { User, RoleManager, dataDictionary } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import { existByRoleCode, existByRoleName, save, editRole } from "@/api/modules/accessController";
import {
  saveGroup,
  mergeGroup,
  dataDictionaryItems,
  isDataDictionaryItemsDnameExisted,
  mergeDataDictionaryItems
} from "@/api/modules/dataDictionary";
const rules = reactive({
  dname: [{ required: true, message: "请输入名称" }]
});

const groupCode = ref("");
let roleStateFlag = ref("");
let color = ref("#ffcc00");
interface DrawerProps {
  title: string;
  isView: boolean;
  groupCode: string;
  row: Partial<dataDictionary.ResDataDictionaryList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  groupCode: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  console.log("接收父组件传过来的参数");
  console.log(params);
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      let param = {
        dno: "",
        dname: "imode",
        description: "",
        remark: "",
        color: "",
        dcode: "",
        groupCode: ""
      };
      param.dno = drawerProps.value.row!.dno == undefined ? "" : drawerProps.value.row!.dno.toString();
      param.dname = drawerProps.value.row!.dname == undefined ? "" : drawerProps.value.row!.dname;
      param.description = drawerProps.value.row!.description == undefined ? "" : drawerProps.value.row!.description;
      param.remark = drawerProps.value.row!.remark == undefined ? "" : drawerProps.value.row!.remark;
      param.color = drawerProps.value.row!.color == undefined ? "" : drawerProps.value.row!.color;
      param.dcode = drawerProps.value.row!.dcode == undefined ? "" : drawerProps.value.row!.dcode;
      param.groupCode = drawerProps.value.groupCode;
      if (`${drawerProps.value.title}` == "新增") {
        let flag = true;
        let param2 = {
          dname: "",
          groupCode: drawerProps.value.groupCode
        };
        param2.dname = drawerProps.value.row!.dname == undefined ? "" : drawerProps.value.row!.dname;
        await isDataDictionaryItemsDnameExisted(param2, "dataDictionaryQueryDto").then(res => {
          if (res.toString() == "") {
          } else {
            flag = false;
          }
        });
        if (flag) {
          await dataDictionaryItems(param, "dataDictionaryItemsDto").then(res => {
            if (res == null) {
              ElMessage.success({ message: `${drawerProps.value.title}成功！` });
              drawerProps.value.getTableList!();
              drawerVisible.value = false;
            } else {
              ElMessage.error({ message: `名称或编号重复` });
            }
          });
        } else {
          ElMessage.error({ message: `名称重复` });
        }
      } else {
        //编辑
        await mergeDataDictionaryItems(param, "dataDictionaryItemsDto").then(res => {
          ElMessage.success({ message: `${drawerProps.value.title}成功！` });
        });
        drawerProps.value.getTableList!();
        drawerVisible.value = false;
      }
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
